
 /**
  * 从地址栏传值的代码
  * @param param
  * @returns
  */
 function getParameter(param){
 	var query = window.location.search;
 	var iLen = param.length;
 	var iStart = query.indexOf(param);
 	if (iStart == -1)
 		return "";
 	iStart += iLen + 1;
 	var iEnd = query.indexOf("&", iStart);
 	if (iEnd == -1)
 		return query.substring(iStart);
 	return query.substring(iStart, iEnd);
 }
 
$(function(){
	$("#manufactureText").val("")
	$("#noNumberText").val(0)
	$("#divTbody").html("")
	var url="../../listenProductionDetail/findProductionDetail.action";
	  var data={"prodId":getParameter("prodId"),"pdStatus":0};
	  $.post(url,data,function(mes){
		  $.each(mes.data,function(i,item){
			  $("#manufactureText").val($("#manufactureText").val()+item.pdId+","+item.muId+","+item.muName+","+item.pdNumber+"#")
				  $("#divTbody").append("<div class='layui-table-body layui-table-main'>" +
						  				 	"<table class='layui-table manufactures' cellspacing='0' cellpadding='0' border='0'>" +
						  				 		"<tbody>" +
						  				 			"<tr>" +
						  				 				"<td style='display:none;'>" +
						  				 						"<div class='layui-table-cell' id='pdId'>"+item.pdId+"</div>"+
						  				 				"</td>"+
						  				 				"<td style='display:none;'>" +
				  				 							"<div class='layui-table-cell' id='muId_"+item.pdId+"'>"+item.muId+"</div>"+
				  				 						"</td>"+
				  				 						"<td style='width: 131px;'>" +
				  				 							"<div class='layui-table-cell' id='muName_"+item.pdId+"'>"+item.muName+"</div>"+
				  				 						"</td>"+
				  				 						"<td style='width: 131px;'>" +
				  				 							"<div class='layui-table-cell' id='pdNumber_"+item.pdId+"'>"+item.pdNumber+"</div>"+
				  				 						"</td>"+
				  				 						"<td style='width: 131px;'>" +
				  				 							"<div class='layui-table-cell' id='mfComplete_"+item.pdId+"'>未完成</div>"+
				  				 						"</td>"+
				  				 						"<td style='width: 131px;'>" +
				  				 							"<div class='layui-table-cell' style='width: 130px;' id='completed_"+item.pdId+"'>0</div>"+
				  				 						"</td>"+
				  				 						"<td style='display:none;'>" +
		  				 									"<div class='layui-table-cell' id='oldNumber"+item.pdId+"'>0</div>"+
		  				 								"</td>"+
				  				 						"<td style='width: 131px;'>" +
				  				 							"<div class='layui-table-cell'>" +
					  				 							"<div class='layui-inline'>"+
																"<input class='layui-input' maxlength='5'"+
																	"style='border: none; padding-bottom: 8px;'"+
																	"name='mpUnfinished' id='completeNumber_"+item.pdId+"'"+
																	"autocomplete='off' placeholder='输入完成数量' value='0'"+
																	"onkeyup='clearNoNum(this,event);'>"+
																"</div>"+
				  				 							"</div>"+
			  				 							"</td>"+
				  				 						"<td style='width: 131px;'>" +
				  				 							"<div class='layui-table-cell' id='mfNoComplete_"+item.pdId+"'>"+item.pdNumber+"</div>"+
				  				 						"</td>"+
				  				 						"<td style='width: 101px;'>" +
				  				 							"<div class='layui-table-cell' id='mfNumber_"+item.pdId+"'>0</div>"+
				  				 						"</td>"+
						  				 			"</tr>"+
						  				 		"</tbody>"+
						  				 	"</table>"+
				  						"</div>")
		  })
	  },"json")
	  
	  setTimeout(() => {
		  var mfComplete=null;
		  var completed=null;
		  
		  var manufactureTexts=new Array();
		  manufactureTexts=$("#manufactureText").val().split('#');
		  $("#manufactureText").val("")
		  var url="../../listenManufacture/findManufactureNumber.action";
		  var data={"prodId":getParameter("prodId")};
		  $.post(url,data,function(mes){
			 $.each(mes,function(i,item){
				 
					if(manufactureTexts[i]){
						 $("#manufactureText").val($("#manufactureText").val()+manufactureTexts[i]+","+(parseInt(item.pdNumber)-parseInt(item.mfNoComplete))+","+0+","+item.mfNoComplete+","+item.mfNumber+"#")
						 $("#noNumberText").val(parseInt($("#noNumberText").val())+parseInt(item.mfNoComplete))

					}
				 $("#completed_"+item.pdId).html(parseInt(item.pdNumber)-parseInt(item.mfNoComplete))
				 $("#mfNoComplete_"+item.pdId).html(item.mfNoComplete)
				 $("#mfNumber_"+item.pdId).html(item.mfNumber)
				 
			 })
		  },'json');
		  
		 
		  var url="../../listenManufacture/findByProdId.action";
		  var data={"prodId":getParameter("prodId"),"pdStatus":"0"};
		  $.post(url,data,function(mes){
			  var manufactureTexts=new Array();
			  manufactureTexts=$("#manufactureText").val().split('#');
			  $("#manufactureText").val("")
				 $.each(mes.data,function(i,item){
					 if(manufactureTexts[i]){
						 $("#manufactureText").val($("#manufactureText").val()+manufactureTexts[i]+","+item.mfComplete+"#")

					}
					 if(item.mfComplete==0){
						 mfComplete="未完成"
					 }else{
						 mfComplete="已完成"
					 }
					 $("#mfComplete_"+item.pdId).html(mfComplete)
					  
				 })
			  },'json');
	}, 500);
	 
	
})
 function clearNoNum(obj,event){
	 
		 var keycode = String.fromCharCode(event.which); 
			var first=$(obj).val().substr(0,1);
			var noFs=/^[0-9]*$/;
			var mfNoCompleteDiv=$(obj).parent().parent().parent().next().children();
			var mfCompletedDiv=$(obj).parent().parent().parent().prev().prev().children();
			var oldNumberDiv=$(obj).parent().parent().parent().prev().children();
			var mfCompleteDiv=$(obj).parent().parent().parent().prev().prev().prev().children();


				if($(obj).val()==""){
					$(obj).val(0)
					$(mfNoCompleteDiv).html(parseInt($(mfNoCompleteDiv).html())+parseInt($(oldNumberDiv).html()))
					$(mfCompletedDiv).html(parseInt($(mfCompletedDiv).html())-parseInt($(oldNumberDiv).html()))
					$(mfCompleteDiv).html("未完成")
					$(oldNumberDiv).html($(obj).val())
				}else{
					if(!noFs.test($(obj).val())){
						$(obj).val(0)
						$(mfNoCompleteDiv).html(parseInt($(mfNoCompleteDiv).html())+parseInt($(oldNumberDiv).html()))
						$(mfCompletedDiv).html(parseInt($(mfCompletedDiv).html())-parseInt($(oldNumberDiv).html()))
						$(mfCompleteDiv).html("未完成")
						$(oldNumberDiv).html($(obj).val())
					}else{
						var mfNoCompleteNumbers=parseInt($(mfNoCompleteDiv).html())+parseInt($(oldNumberDiv).html())
						var mfCompleteNumbers=parseInt($(mfCompletedDiv).html())-parseInt($(oldNumberDiv).html())
						if(first==0){
							
							CalculationNumber(obj,keycode,oldNumberDiv,mfNoCompleteDiv,mfCompletedDiv,mfNoCompleteNumbers,mfCompleteNumbers,mfCompleteDiv);
						}else{
							CalculationNumber(obj,keycode,oldNumberDiv,mfNoCompleteDiv,mfCompletedDiv,mfNoCompleteNumbers,mfCompleteNumbers,mfCompleteDiv);
						}
					}
				}
				addText(obj);
				var manufactureTexts=new Array();
				manufactureTexts=$("#manufactureText").val().split("#");
				$("#noNumberText").val(0)
				for (var i = 0; i < manufactureTexts.length; i++) {
					if(manufactureTexts[i]){
						$("#noNumberText").val(parseInt($("#noNumberText").val())+parseInt(manufactureTexts[i].split(",")[6]));
					}
				}
	 }

function CalculationNumber(obj,keycode,oldNumberDiv,mfNoCompleteDiv,mfCompletedDiv,mfNoCompleteNumbers,mfCompleteNumbers,mfCompleteDiv){
	if(parseInt($(obj).val())>mfNoCompleteNumbers){
		layer.msg("对不起不能超过剩余数量")
		$(obj).val(0)
		$(mfNoCompleteDiv).html(parseInt($(mfNoCompleteDiv).html())+parseInt($(oldNumberDiv).html()))
		$(mfCompletedDiv).html(parseInt($(mfCompletedDiv).html())-parseInt($(oldNumberDiv).html()))
		$(mfCompleteDiv).html("未完成")
		$(oldNumberDiv).html($(obj).val())
	}else if(parseInt($(obj).val())==mfNoCompleteNumbers){
		var first=$(obj).val().substr(0,1);
		var number=null;
		if(first==0){
			number=keycode
		}else{
			number=$(obj).val()
		}
		$(obj).val("")
		$(obj).val(number)
		$(mfNoCompleteDiv).html(0)
		$(mfCompletedDiv).html(parseInt(mfCompleteNumbers)+parseInt($(obj).val()))
		$(mfCompleteDiv).html("已完成")
		$(oldNumberDiv).html($(obj).val())
	}else{
		var first=$(obj).val().substr(0,1);
		var number=null;
		if(first==0){
			number=keycode
		}else{
			number=$(obj).val()
		}
		$(obj).val("")
		$(obj).val(number)
		$(mfNoCompleteDiv).html(parseInt(mfNoCompleteNumbers)-parseInt($(obj).val()))
		$(mfCompletedDiv).html(parseInt(mfCompleteNumbers)+parseInt($(obj).val()))
		$(mfCompleteDiv).html("未完成")
		$(oldNumberDiv).html($(obj).val())
	}
}
layui.use('table', function(){
	  var table = layui.table;
	   
	   
	 
	   
	  setTimeout(() => {
		  var $ = layui.$, active = {
				    reload: function(){
				      var muId=$("#muId");
				      //执行重载    查询
				      table.reload('manufacture', {
				        page: {
				          curr: 1 //重新从第 1 页开始
				        }
				        ,where: {
				        	prodId: getParameter("prodId"),
				        	muId: muId.val()
				        }
				      });
				      
				      table.reload('manufactures', {
					        page: {
					          curr: 1 //重新从第 1 页开始
					        }
					        ,where: {
					        	prodId: getParameter("prodId"),
					        	muId: muId.val()
					        }
					      });
				    }
				  };
		  active['reload'] ? active['reload'].call(this) : '';
		  
		  $('.demoTable #search').on('click', function(){
		    var type = $(this).data('type');
		    active[type] ? active[type].call(this) : '';
		  });
	  }, 200);
	  //表单重载
	  
	  
	 
	});
layui.use(['form', 'table'], function(){
	  var form = layui.form
	  var table = layui.table
	  var layer = layui.layer
	  
	var url="../../listenMusical/selectAll.action";
	var data=null
	$.post(url,data,function(mes){
		 $.each(mes.data,function(i,item){
				 $("#muId").append("<option value="+item.muId+">"+item.muName+"</option>")
				  load();
		  })
	},"json");
	
	
	$("#submitDd").click(function(){
		var boolean=false;
		var manufactureTexts=new Array();
		manufactureTexts=$("#manufactureText").val().split('#');
	    for (var i = 0; i < manufactureTexts.length; i++) {
	    	if(manufactureTexts[i]){
	    		var number=$("#completeNumber_"+manufactureTexts[i].split(",")[0]).val()
				if(parseInt(number)<1){
					boolean=false;
					break ;
				}else{
					boolean=true;
				}
	    	}
	    	
		}
	    if(boolean){
	    	var url="../../listenManufacture/addManufacture.action";
			var data={"manufactureText":$("#manufactureText").val(),"noNumber":$("#noNumberText").val(),"prodId":getParameter("prodId")}
			$.post(url,data,function(mes){
				if(mes>0){
					  window.parent.location.reload()
					  var index=parent.layer.getFrameIndex(window.name);
					  parent.layer.close(index);
				  }
			},"json");
	    }else{
	    	layer.msg("请输入制造乐器数量,不能小于0")
	    }
		
	})
	  
});

function addText(obj){
	 $("#manufactureText").val("")
	 var mfComplete=null;
		  $.each($(".manufactures"),function(i,item){
			  var id=$(item).find("tr").find("td").get(0)
			  var ids=$(id).children().html()
			  if($("#mfComplete_"+ids).html()=="已完成"){
				  mfComplete=1
			  }else{
				  mfComplete=0;
			  }
			   $("#manufactureText").val($("#manufactureText").val()+ids+","+$("#muId_"+ids).html()+","+$("#muName_"+ids).html()+","+$("#pdNumber_"+ids).html()+","+$("#completed_"+ids).html()+","+$("#completeNumber_"+ids).val()+","+$("#mfNoComplete_"+ids).html()+","+$("#mfNumber_"+ids).html()+","+mfComplete+"#")
		  })
	 
	
}
function load(){
	var form = layui.form;
	form.render();
}